﻿using System;
using System.Collections.Generic;

namespace Mono.Cecil.PE {

	sealed class ByteBufferEqualityComparer : IEqualityComparer<ByteBuffer> {

		public static readonly ByteBufferEqualityComparer Instance = new ByteBufferEqualityComparer ();

		public bool Equals (ByteBuffer x, ByteBuffer y)
		{
			if (x == null || y == null)
				return x == y;

			if (x == y)
				return true;

			if (x.length != y.length)
				return false;

			var x_buffer = x.buffer;
			var y_buffer = y.buffer;

			for (int i = 0; i < x.length; i++)
				if (x_buffer [i] != y_buffer [i])
					return false;

			return true;
		}

		public int GetHashCode (ByteBuffer buffer)
		{
			var hash = 0;
			var bytes = buffer.buffer;
			for (int i = 0; i < bytes.Length; i++)
				hash = (hash * 37) ^ bytes [i];

			return hash;
		}
	}
}
